//
//  SWIndexViewController.swift
//  ChildStory
//
//  Created by linhan on 16/7/15.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWIndexViewController: SWBaseViewController,SWImagePickerControllerDelegate,SWTakePhotoViewControllerDelegate,SWAPIComponent
{
    
    private var _nameLabel:UILabel = UILabel()
    private var _avatarView:ImageLoader = ImageLoader(frame: CGRectZero)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    override func viewWillLayoutFirstTime(){
        //setup()
    }
    
    private func setup()
    {
        print("index frame:", view.frame)
        view.backgroundColor = UIColor.white
        
        
        
        let settingBtn:UIButton = UIButton(type:.system)
        settingBtn.setTitle("设置", for: .normal)
        settingBtn.titleLabel?.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        settingBtn.sizeToFit()
        settingBtn.frame = CGRectMake(20, view.height - 50, settingBtn.width, settingBtn.height)
        settingBtn.addTarget(self, action: #selector(settingBtnTapped), for: .touchUpInside)
        view.addSubview(settingBtn)
        
        let takePhotoBtn:UIButton = UIButton(type:.system)
        takePhotoBtn.setTitle("拍照", for: .normal)
        takePhotoBtn.titleLabel?.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        takePhotoBtn.sizeToFit()
        takePhotoBtn.frame = CGRectMake(settingBtn.right + 20, view.height - 50, takePhotoBtn.width, takePhotoBtn.height)
        takePhotoBtn.addTarget(self, action: #selector(takePhotoBtnTapped(_:)), for: .touchUpInside)
        view.addSubview(takePhotoBtn)
        
        let multiTakePhotoBtn:UIButton = UIButton(type:.system)
        multiTakePhotoBtn.tag = 1
        multiTakePhotoBtn.setTitle("多拍", for: .normal)
        multiTakePhotoBtn.titleLabel?.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        multiTakePhotoBtn.sizeToFit()
        multiTakePhotoBtn.frame = CGRectMake(settingBtn.right + 20, view.height - 90, takePhotoBtn.width, takePhotoBtn.height)
        multiTakePhotoBtn.addTarget(self, action: #selector(takePhotoBtnTapped(_:)), for: .touchUpInside)
        view.addSubview(multiTakePhotoBtn)
        
        let pickerBtn:UIButton = UIButton(type:.system)
        pickerBtn.setTitle("单选", for: .normal)
        pickerBtn.titleLabel?.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        pickerBtn.sizeToFit()
        pickerBtn.frame = CGRectMake(settingBtn.right + 20, view.height - 160, takePhotoBtn.width, takePhotoBtn.height)
        pickerBtn.addTarget(self, action: #selector(pickerBtnTapped(_:)), for: .touchUpInside)
        view.addSubview(pickerBtn)
        
        let multiPickerBtn:UIButton = UIButton(type:.system)
        multiPickerBtn.tag = 1
        multiPickerBtn.setTitle("多选", for: .normal)
        multiPickerBtn.titleLabel?.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        multiPickerBtn.sizeToFit()
        multiPickerBtn.frame = CGRectMake(settingBtn.right + 20, view.height - 130, takePhotoBtn.width, takePhotoBtn.height)
        multiPickerBtn.addTarget(self, action: #selector(pickerBtnTapped(_:)), for: .touchUpInside)
        view.addSubview(multiPickerBtn)
        
        let albumBtn:UIButton = UIButton(type:.system)
        albumBtn.setTitle("相册", for: .normal)
        albumBtn.titleLabel?.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        albumBtn.sizeToFit()
        albumBtn.frame = CGRectMake(takePhotoBtn.right + 20, view.height - 50, albumBtn.width, albumBtn.height)
        albumBtn.addTarget(self, action: #selector(albumBtnTapped), for: .touchUpInside)
        view.addSubview(albumBtn)
        
        let inviteBtn:UIButton = UIButton(type:.system)
        inviteBtn.setTitle("邀请", for: .normal)
        inviteBtn.titleLabel?.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        inviteBtn.sizeToFit()
        inviteBtn.frame = CGRectMake(albumBtn.right + 20, view.height - 50, inviteBtn.width, inviteBtn.height)
        inviteBtn.addTarget(self, action: #selector(inviteBtnTapped), for: .touchUpInside)
        view.addSubview(inviteBtn)
        
        let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(avatarViewTapped))
        _avatarView.frame = CGRectMake(20, 10, 40, 40)
        _avatarView.options.cornerRadius = 20
        _avatarView.load(SWAccountManager.sharedManager().account?.avatarURL ?? "")
        _avatarView.isUserInteractionEnabled = true
        _avatarView.addGestureRecognizer(tapGesture)
        view.addSubview(_avatarView)
        
        _nameLabel.frame = CGRectMake(_avatarView.right, _avatarView.center.y, 100, 20)
        _nameLabel.textColor = UIColor.darkGray
        _nameLabel.font = SWAppearanceManager.themeStyle.fontOfSize(13)
        _nameLabel.text = SWAccountManager.sharedManager().account?.screenName ?? SW.Config.DefaultKidsName
        view.addSubview(_nameLabel)
        
        
    }
    
    
    func imagePickerController(_ picker: SWImagePickerController, didFinishPickingMediaWithPhotos photos: [SWALPhoto])
    {
        //添加全局记录
        print("添加前:", SWAssetsManager.sharedManager().assets.count)
        let localIdentifiers:[String] = photos.flatMap({$0.localIdentifier}).filter({$0 != ""})
        SWAssetsManager.sharedManager().addAssets(localIDs: localIdentifiers, event: nil)
        print("添加后:", SWAssetsManager.sharedManager().assets.count)
        picker.close()
    }
    
    func imagePickerControllerDidCancel(_ picker: SWImagePickerController)
    {
        picker.close()
    }
    
    func takePhotoViewController(_ viewController:SWTakePhotoViewController, didFinishPicking images:[UIImage])
    {
        if let image = images.valueAt(0)
        {
            SWAssetsLibraryHelper.shared().writeImageDataToAlbum(image: image, albumName: nil, completionHandler: {result in
                
                DispatchQueue.main.async {
                    SVProgressHUD.showSuccess(withStatus: "已保存到相册")
                }
                
            })
        }
    }
    
    
    
    private func fetchStages()
    {
        
    }
    
    
    
    @objc private func historyBtnTapped()
    {
        //navigationController?.pushViewController(SWHistoryViewController(), animated: true)
    }
    
    @objc private func settingBtnTapped()
    {
        navigationController?.pushViewController(SWGeneralSettingViewController(), animated: true)
    }
    
    @objc private func takePhotoBtnTapped(_ button:UIButton)
    {
        /**
        let editorViewController = SWPhotoEditorEntranceViewController()
        let navigation = SWBaseNavigationController(rootViewController: editorViewController)
        
        editorViewController.successCallback = {[weak navigation] url in
            navigation?.dismiss(animated:true, completion: {
                
            })
        }
        
        present(navigation, animated: true, completion:nil)
         **/
        
        let viewController = SWTakePhotoViewController()
        viewController.delegate = self
        if button.tag == 1
        {
            //viewController.multy = true
        }
        let navigation = SWBaseNavigationController(rootViewController: viewController)
        present(navigation, animated: true, completion:nil)
    }
    
    @objc private func pickerBtnTapped(_ button:UIButton)
    {
        let pickerController = SWImagePickerController()
        pickerController.delegate = self
        
        if button.tag == 1
        {
            pickerController.allowsMultipleSelection = true
            pickerController.maxCount = 3
        }
        
        let navigation = SWBaseNavigationController(rootViewController: pickerController)
        present(navigation, animated: true, completion:nil)
    }
    
    @objc private func albumBtnTapped()
    {
        navigationController?.pushViewController(SWAlbumViewController(), animated: true)
        //navigationController?.pushViewController(SWChildAddViewController(), animated: true)
    }
    
    @objc private func avatarViewTapped()
    {
        if let children = SWAccountManager.sharedManager().account?.children as? [SWChild],
           let child = children.valueAt(0)
        {
            navigationController?.pushViewController(SWChildProfileSettingViewController(child: child), animated: true)
        }
        //navigationController?.pushViewController(SWChildInfoSupplementViewController(), animated: true)
    }
    
    @objc private func inviteBtnTapped()
    {
        navigationController?.pushViewController(SWInvitationViewController(), animated: true)
        //navigationController?.pushViewController(SWInvitationHandleViewController(), animated: true)
    }
    
    
    
    
    
    
}
